package com.ruoyi.msg.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.msg.domain.bo.MsgQaBo;
import com.ruoyi.msg.domain.vo.MsgQaVo;
import com.ruoyi.msg.service.IMsgQaService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 答疑解惑
 *
 * @author xiaohao
 * @date 2023-02-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/msg/qa")
public class MsgQaController extends BaseController {

    private final IMsgQaService iMsgQaService;

    /**
     * 查询答疑解惑列表
     */
    @SaCheckPermission("msg:qa:list")
    @GetMapping("/list")
    public TableDataInfo<MsgQaVo> list(MsgQaBo bo, PageQuery pageQuery) {
        return iMsgQaService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出答疑解惑列表
     */
    @SaCheckPermission("msg:qa:export")
    @Log(title = "答疑解惑", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MsgQaBo bo, HttpServletResponse response) {
        List<MsgQaVo> list = iMsgQaService.queryList(bo);
        ExcelUtil.exportExcel(list, "答疑解惑", MsgQaVo.class, response);
    }

    /**
     * 获取答疑解惑详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("msg:qa:query")
    @GetMapping("/{id}")
    public R<MsgQaVo> getInfo(@NotNull(message = "主键不能为空")
                              @PathVariable Long id) {
        return R.ok(iMsgQaService.queryById(id));
    }

    /**
     * 新增答疑解惑
     */
    @SaCheckPermission("msg:qa:add")
    @Log(title = "答疑解惑", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MsgQaBo bo) {
        return toAjax(iMsgQaService.insertByBo(bo));
    }

    /**
     * 修改答疑解惑
     */
    @SaCheckPermission("msg:qa:edit")
    @Log(title = "答疑解惑", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MsgQaBo bo) {
        return toAjax(iMsgQaService.updateByBo(bo));
    }

    /**
     * 删除答疑解惑
     *
     * @param ids 主键串
     */
    @SaCheckPermission("msg:qa:remove")
    @Log(title = "答疑解惑", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iMsgQaService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
